package com.atguigu.domain.credit.service;

import com.atguigu.domain.credit.model.aggregate.TradeAggregate;
import com.atguigu.domain.credit.model.entity.CreditAccountEntity;
import com.atguigu.domain.credit.model.entity.CreditOrderEntity;
import com.atguigu.domain.credit.model.entity.TradeEntity;
import com.atguigu.domain.credit.repository.ICreditRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @description 积分调额服务【正逆向，增减积分】
 * @Date 2025/6/7 12:55
 * @Created by 27810
 */
@Slf4j
@Service
public class CreditAdjustService implements ICreditAdjustService{
    @Resource
    private ICreditRepository creditRepository;
    @Override
    public String createOrder(TradeEntity tradeEntity) {
        String userId = tradeEntity.getUserId();

        CreditOrderEntity creditOrderEntity = TradeAggregate.createCreditOrderEntity(userId, tradeEntity.getTradeName(), tradeEntity.getTradeType(), tradeEntity.getAmount(), tradeEntity.getOutBusinessNo());
        CreditAccountEntity creditAccountEntity = TradeAggregate.createCreditAccountEntity(userId, tradeEntity.getAmount());
        TradeAggregate tradeAggregate = new TradeAggregate(userId, creditAccountEntity, creditOrderEntity);

        creditRepository.saveUserCreditTradeOrder(tradeAggregate);
        log.info("增加账户积分额度完成 userId:{} orderId:{}", tradeEntity.getUserId(), creditOrderEntity.getOrderId());

        return creditOrderEntity.getOrderId();
    }
}
